var app=getApp();
var fakedata=require("../../static/js/fakedata");
Page({
    data: {
        url:'../../static/images/icon_status-dot.png',
        displayList:[],
        userid:''
    },
    showmsg(){
        swan.showModal({
            title:"此商品还没有被人预定！"
        });
    },
    //显示买家信息
    connectbuyer(e){
        let nickname=e.currentTarget.dataset.buyername;
        let phone=e.currentTarget.dataset.buyerphone;
        swan.showModal({
            title: '买家信息',
            content: "昵称："+nickname+"\n手机号："+phone,
            showCancel: false,
        });
    },
    // 完成交易
    confirmdeal(e){
        let goodid=e.currentTarget.dataset.goodid;
        swan.request({
            url: fakedata.urls.comfirmdeal,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'json',
            data: {
                goodid:goodid
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    swan.showModal({
                        title: '提示',
                        content: '交易已经完成！'
                    });
                    this.setData({
                        canbuy:false
                    });
                    this.updata();
                }else{
                    swan.showModal({
                        title:'错误'
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    },
    onLoad(){
        if(app.globalData.islogin){
            this.setData({
                userid:app.globalData.userinfo.id
            });
        }
    },
    onShow(){
        if(!app.globalData.islogin){
            swan.showModal({
                title:'您还没有登录'
            });
            swan.navigateTo({
                url:'/pages/login/login'
            });
        }else{
            this.setData({
                userid:app.globalData.userinfo.id
            });
            swan.request({
                url: fakedata.urls.getmypublished,
                header: {
                    'Content-Type': 'application/json',
                    'Developer-Server-Domain': fakedata.host,
                    'X-SP-Accept-Encoding': 'base64'
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                data: {
                    userid:this.data.userid
                },
                success: res => {
                    console.log(res);
                    if(res.statusCode==200){
                        this.setData({
                            displayList:res.data.data
                        });
                    }else{
                        swan.showModal({
                            title:"出错啦"
                        });
                    }
                },
                fail: err => {
                    swan.showModal({
                        title: '错误信息',
                        content: JSON.stringify(err)
                    });
                }
            });
        }
    },
    updata(){
        swan.request({
            url: fakedata.urls.getmypublished,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            data: {
                userid:this.data.userid
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    this.setData({
                        displayList:res.data.data
                    });
                }else{
                    swan.showModal({
                        title:"出错啦"
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    }
});